﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Forgot_Password : System.Web.UI.Page
{
    SendEmails autoMail = new SendEmails();
    RegistrationData regdata = new RegistrationData();
    string Link = string.Empty, strCode = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        strCode = Guid.NewGuid().ToString();
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        if (txtEmail.Text == string.Empty)
        {
            lblMessage.Text = "Please Enter a valid email address";
        }
        else
        {
            SqlDataReader dr;
            dr = regdata.CheckEmail(txtEmail.Text);
            if (dr.HasRows)
            {
                if (dr.Read())
                {
                    //URL for Mail
                    string UrlWithoutPageName = HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.LastIndexOf("/") + 1);
                    Link = UrlWithoutPageName + "Reg-ResetPassword.aspx";
                    SendResetPasswordMail();
                    //lblMessage.Text = "Please check your E-Mail. A link for recovering the password has been sent to your address.";
                    //btnCancel.Value = "Close";
                    //to set IsForgotPassword flag in USerMaster
                    string strConfig = string.Empty;
                    strConfig = EmailData.WebsiteLink;
                    string Link1 = strConfig + "/ResetPassword-";
                    Link1 = Link1 + strCode;
                    regdata.UpdateUserMaster_IsForgotPassword(txtEmail.Text, strCode);

                    string script = string.Format("javascript:parent.$.fancybox.close();");
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alertScript", script, true);
                }
                else
                {
                    lblMessage.Text = "Sorry the E-mail address '" + txtEmail.Text + "' is not found in our records.";
                }
            }
            else
            {
                lblMessage.Text = "Sorry the E-mail address '" + txtEmail.Text + "' is not found in our records.";
            }
        }
    }

    protected void SendResetPasswordMail()
    {
        string strConfig = string.Empty;
        strConfig = EmailData.WebsiteLink;
        string Link = strConfig + "Reg-ResetPassword.aspx?id=";
        Link = Link + strCode;
        string subject = EmailData.WebsiteLink + " - Reset Password";
        EmailData.Link = Link;
        autoMail.GeneralEmail(subject, txtEmail.Text, 2);
    }
     
}